<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
require_once APPPATH . 'controllers/application' . EXT;

class Product extends Application {

    var $terms = array();

    public function __construct() {
        parent::__construct();
    }

    public function san_pham() {
        $data = array();
        $data["mid"] = $this->uri->segment(2);
        $data["sx"] = $this->uri->segment(3);
        //cofig phan trang
        $result_per_page = 15;
        if ($data["sx"]) {
            $config['base_url'] = base_url() . 'tat-ca-san-pham/'.$data["mid"] ."/". $data["sx"];
        } else {
            $config['base_url'] = base_url() . 'tat-ca-san-pham/'.$data["mid"].'/NULL';
        }
        $config['total_rows'] = $this->product_model->GetTotalPd($data["mid"]);
        $config['per_page'] = $result_per_page;
        $config['uri_segment'] = 4;
        $this->cfPag();
        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();
        //ket thuc phan trang
        $m = $this->menu_model->getMenuFromID($data["mid"])->row();
        $this->titlePage = $m->menu_name;
        $this->breadcrumb->add_crumb($this->home,base_url().'/main');
        $this->breadcrumb->add_crumb($m->menu_name, '');

        $data['product_all'] = $this->product_model->selectAllP(array('FStatus' => 0,"menu_id"=>$data["mid"]), $data["sx"], $result_per_page, $this->uri->segment(4))->result();
        $this->render($this->load->view('productall', $data, true));
    }

    public function danh_muc() {
        $data = array();
//        $pid = $this->uri->segment(3);
        $data["sx"] = $this->uri->segment(3);
        
        $pid = (int) end(explode("-", $this->uri->segment(2)));
        $data["p"] = $this->product_model->GetCategory($pid)->row();
        $data['pname'] = $data["p"]->PName;
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb($this->home, '/dienhoa2/main');
        $this->breadcrumb->add_crumb($data["p"]->PName, '');

        //cofig phan trang
        $result_per_page = 10;
        if ($data["sx"]) {
            $config['base_url'] = base_url() . 'danh-muc/' . $data["p"]->c_slug . '-' . $pid . '/' . $data["sx"];
        } else {
            $config['base_url'] = base_url() . 'danh-muc/' . $data["p"]->c_slug . '-' . $pid . '/NULL';
        }
        $config['total_rows'] = $this->product_model->GetTotalToCat($pid);
        $config['per_page'] = $result_per_page;
        $config['uri_segment'] = 4;
        $this->cfPag();

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();
        //ket thuc phan trang
        $this->titlePage = $data["p"]->PName;

        $data['p_from_cat'] = $this->product_model->getProductFromCat($pid, $data["sx"], $result_per_page, $this->uri->segment(4))->result();
        $this->render($this->load->view('product_cat', $data, true));
    }

    public function detail() {
        $data = array();

//        $fid = $this->uri->segment(3);
        $fid = (int) end(explode("-", $this->uri->segment(2)));
        $data["fname"] = $this->product_model->getProductName($fid);
        $pid = $this->product_model->getProduct($fid)->row();
        $p = $this->product_model->GetCategory($pid->PID)->row();
        $data["sptt"] = $this->product_model->getSameProduct($fid, $p->PID)->result();
        $data["fid"] = $fid;
        $data["com"] = $this->com_model->getCom($fid)->result();
        if ($this->session->userdata("logged_in")) {
            $sess = $this->session->userdata("logged_in");
            $data["name"] = $sess["username"];
        }
        $this->breadcrumb->clear();
        $this->breadcrumb->add_crumb($this->home, '/dienhoa2/main');
        $this->breadcrumb->add_crumb($p->PName, '/dienhoa2/danh-muc/' . $p->c_slug . '-' . $p->PID);
        $this->breadcrumb->add_crumb($data["fname"], '');

        $this->titlePage = $data["fname"];
        $data['p_detail'] = $this->product_model->getProduct($fid)->result();
        $data['PdMostView'] = $this->product_model->getProduct($fid, TRUE)->result();

        $this->render($this->load->view('detail_view', $data, true));
    }


    public function SearchItem() {
        $limit = 10;
        $offset = $this->uri->segment(7);
        $total_segment = $this->uri->total_segments();
        $default = array('cate', 'key');
        if ($total_segment > 3) {
            $query_array = array();
            $seg = $this->uri->uri_to_assoc(3, $default);
            for ($i = 0; $i < count($default); $i++) {
                if ($seg[$default[$i]] === 'unset') {
                    $seg[$default[$i]] = '';
                    $query_array[$default[$i]] = 'unset';
                } else {
                    $query_array[$default[$i]] = $seg[$default[$i]];
                }
            }
            $str = $this->uri->assoc_to_uri($query_array);
            $result = $this->product_model->search($seg, $limit, $offset);
        } else {
            $query_array = array(
                'cate' => $this->input->post('categories'),
                'key' => $keys = $this->input->post('txtsearch')
            );
            for ($i = 0; $i < count($default); $i++) {
                if ($query_array[$default[$i]] === '') {
                    $query_array[$default[$i]] = 'unset';
                    $query_array_uri[$default[$i]] = '';
                } else {
                    $query_array_uri[$default[$i]] = $query_array[$default[$i]];
                }
            }
            $str = $this->uri->assoc_to_uri($query_array);
            $result = $this->product_model->search($query_array_uri, $limit, $offset);
        }

        $data['flower'] = $result['rows'];
        $data['num_results'] = $result['num_rows'];
        //cofig
        $config = array();
        $config['base_url'] = base_url() . 'product/SearchItem/' . $str;
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 7;
        $this->cfPag();
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $this->render($this->load->view('search_result_view', $data, TRUE));
    }

    public function UserComment() {
        if ($this->session->userdata("logged_in")) {
            //check form
            $this->form_validation->set_rules("cm_text", "nội dung", "required|max_length[300]");
            if ($this->form_validation->run() === FALSE) {
                $infor = array(
                    "status" => false,
                    "errors" => form_error("cm_text")
                );
                echo json_encode($infor);
                die;
//                echo "khong gui dc2";
            } else {
                $fid = $this->uri->segment(3);
                $session = $this->session->userdata("logged_in");
                $ngay = date("d");
                $thang = date("m");
                $nam = date("Y");
                $gio = date("H");
                $giay = date("i");
                $time = $ngay . "-" . $thang . "-" . $nam . " " . $gio . ":" . $giay;
                //nhap du lieu
                $query = array(
                    "fid" => $fid,
                    "cm" => $this->input->post("cm_text"),
                    "id" => $session["id"],
                    "curdate" => $nam . "-" . $thang . "-" . $ngay . " " . $gio . ":" . $giay,
                    "name" => $session["username"]
                );
                $rs = $this->com_model->insertCom($query);
                $data["com"] = $this->com_model->getCom($fid)->result();
                if ($rs) {
                    $str = "<p>   
                                <b>".$query['name']. "<span class='cm_time'> ".$time."</span></b>
                                <span class='cm_text'>" . $query['cm'] . "</span>
                            </p>";
                    $infor = array(
                        "status" => true,
                        "cm_content" => $str
                    );
                    echo json_encode($infor);
                    die;
                } else {
                    echo "khong gui dc";
                }
            }
        } else {
            $infor = array(
                "status" => false,
                "errors" => "Bạn cần đăng nhập để sử dụng chức năng bình luận của website! "
            );
            echo json_encode($infor);
            die;
        }
    }
}

?>
